package com.teach;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;

import com.teach.exception.CodeException;
import com.teach.vo.LoginUser;

import cn.hutool.core.util.StrUtil;

/**
 * 常量
 * @author ASUS
 *
 */
public class Const {

	private Const() {}
	
	/**
	 * 登录用户在会话中的属性名
	 */
	public static final String LOGIN_USER_SESSION_NAME = "user";
	
	/**
	 * 获取在会话中存放的验证码的属性名
	 */
	public static final String CODE_NAME = "_CODE_NAME";
	
	public static final LoginUser getLoginUser(HttpSession session) {
		return (LoginUser)session.getAttribute(LOGIN_USER_SESSION_NAME);
	}
	
	/**
	 * 验证码的服务器验证
	 */
	public static void codeVerify(HttpSession session,String code) throws CodeException{
		System.out.println(session.getAttribute(CODE_NAME));
		if(StrUtil.isEmpty(code)) {
			throw new CodeException(18,"验证码不能为空");
		}
		if(session.getAttribute(CODE_NAME)==null) {
			throw new CodeException(11,"验证码已失效");
		}
		if(!code.equals(session.getAttribute(CODE_NAME))) {
			throw new CodeException(12,"验证码不匹配");
		}
	}
}
